package com.onetoall.yjt;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Map;

/**
 * Created by DELL on 2017/2/13.
 */

public class ZhifuActivity extends Activity {

    private Button btn;
    private WebView webView;
    private JavaScriptObject jsobj;
    private IWXAPI api;
    private StringBuffer sb;
    private Map<String, String> resultunifiedorder;
    private PayReq req;
    private Button tj;
    private WXPayUtils payUtils;
    private String mUrl = "http://beta.wx.1toall.net/Test/index?mobile=%s&status=%s&sign=%s";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_updata_zhifu);
        btn = (Button) findViewById(R.id.zhifu);
        tj = (Button) findViewById(R.id.tj);
        webView = (WebView) findViewById(R.id.wv);
        webView.getSettings().setDefaultTextEncodingName("utf-8");
        webView.getSettings().setJavaScriptEnabled(true);
        jsobj = new JavaScriptObject(this);
        webView.addJavascriptInterface(jsobj, "myObj");
        webView.loadUrl("file:///android_asset/WKJSPOST.html");
//        webView.loadUrl("http://www.baidu.com");
        webView.getSettings().setLoadWithOverviewMode(true);
        // webSettings.setUseWideViewPort(true);
        // 设置可以支持缩放
        webView.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        webView.getSettings().setBuiltInZoomControls(true);
        // 扩大比例的缩放
        webView.getSettings().setUseWideViewPort(true);
//		webSettings.setUserAgentString("http://www.zmei.com");
        // 自适应屏幕
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        /**带自动参数的调用事例
         String msg = ((EditText) findViewById(R.id.input_et)).getText().toString();
         //调用js中的函数：showInfoFromJava(msg)
         webView.loadUrl("javascript:showInfoFromJava('" + msg + "')");*/
//        final String string = String.format("{\"userInfo\":\"{\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\"}\"}", "userId", "289187","status","1","mobile","18936172702");
        mUrl = String.format(mUrl,"18936172702","1", MD5.getMessageDigest("liuyixiang".getBytes()));
        Log.e("mUrl",mUrl);
        final String url = "http://beta.wx.1toall.net/Test/index";
        final String string = String.format("{\"userInfo\":\"{%s:%s}\"}", "userId", "289187");
        Log.e("string", string);
        webView.loadUrl("javascript:post('" + url + "','" + string + "')");
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl("file:///android_asset/WKJSPOST.html");
                return true;
            }
        });
        sb=new StringBuffer();
        req=new PayReq();
        api = WXAPIFactory.createWXAPI(this, Contants.APP_ID);

        payUtils = new WXPayUtils(ZhifuActivity.this);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                postJS();
                webView.loadUrl(mUrl);
//                Log.e("TAG","javascript:post('" + url + "','" + string + "')");
//                webView.loadUrl("http://beta.wx.1toall.net/Test/index");
//                webView.loadUrl("javascript:post('" + url + "','" + string + "')");
//               webView.loadUrl("javascript:post('http://beta.wx.1toall.net/Theme/index','{\"key\":\"value\"}')");
//                String postUrl = "javascript:post('http://beta.wx.1toall.net/Theme/index',"+string+")";
//                Log.e("TAG",postUrl);
//                webView.loadUrl(postUrl);
//                webView.loadUrl("javascript:android()");
//                String urlString = "https://api.mch.weixin.qq.com/pay/unifiedorder";


            }
        });
        tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }


    void postJS() {
        RequestParams params = new RequestParams("https://www.baidu.com/s");
        params.addQueryStringParameter("wd", "xUtils");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Toast.makeText(x.app(), "onSuccess", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(x.app(), "onError", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {
                Toast.makeText(x.app(), "onFinished", Toast.LENGTH_LONG).show();
            }
        });
    }

    class JavaScriptObject {
        Context mContext;

        public JavaScriptObject(Context zhifuActivity) {
            mContext = zhifuActivity;
        }

        @JavascriptInterface
        public void fun1FromAndroid(String name) {
            Log.e("TAG订单号:",name);
            Toast.makeText(mContext, name, Toast.LENGTH_LONG).show();
//
            try {
                payUtils.doPay(name);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @JavascriptInterface
        public void fun2(String name) {

        }
    }
















}
